package com.xja.manager.web;

import com.xja.manager.base.PageResponse;
import com.xja.manager.base.ResponseResult;
import com.xja.manager.dto.NursingProjectDto;
import com.xja.manager.service.NursingProjectService;
import com.xja.manager.vo.NursingProjectVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

import static com.xja.manager.base.ResponseResult.success;

/**
 * 护理项目Controller类
 * 该类提供对护理项目信息的增、删、改、查操作。
 */
@RestController
@RequestMapping("/nursing_project")
@Api(tags = "护理项目管理接口")
public class NursingProjectController {

    @Autowired
    private NursingProjectService nursingProjectService;

    /**
     * 分页查询护理项目列表
     *
     * @param name 护理项目名称（可选）
     * @param status 状态：0-禁用，1-启用（可选）
     * @param pageNum 当前页码（默认：1）
     * @param pageSize 每页显示的记录数（默认：10）
     * @return 分页查询结果
     */
    @GetMapping
    @ApiOperation(value = "分页查询护理项目列表", notes = "根据名称和状态进行分页查询护理项目。")
    public ResponseResult<PageResponse<NursingProjectVo>> getByPage(
            @ApiParam(value = "护理项目名称", required = false)
            @RequestParam(value = "name", required = false) String name,

            @ApiParam(value = "状态：0-禁用，1-启用", required = false)
            @RequestParam(value = "status", required = false) Integer status,

            @ApiParam(value = "当前页码", defaultValue = "1")
            @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,

            @ApiParam(value = "每页显示数量", defaultValue = "10")
            @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize
    ) {
        PageResponse<NursingProjectVo> nursingProjectPageInfo = nursingProjectService.getByPage(name, status, pageNum, pageSize);
        return ResponseResult.success(nursingProjectPageInfo);
    }

    /**
     * 新增护理项目
     *
     * @param nursingProjectDTO 护理项目数据传输对象
     * @return 操作结果
     */
    @PostMapping
    @ApiOperation(value = "新增护理项目", notes = "添加新的护理项目")
    public ResponseResult add(
            @ApiParam(value = "护理项目数据传输对象", required = true)
            @RequestBody NursingProjectDto nursingProjectDTO) {
        nursingProjectService.add(nursingProjectDTO);
        return ResponseResult.success();
    }

    /**
     * 根据编号查询护理项目信息
     *
     * @param id 护理项目编号
     * @return 护理项目信息
     */
    @GetMapping("/{id}")
    @ApiOperation(value = "根据编号查询护理项目信息", notes = "通过护理项目编号查询详细信息")
    public ResponseResult<NursingProjectVo> getById(
            @ApiParam(value = "护理项目编号", required = true)
            @PathVariable("id") Long id) {
        NursingProjectVo nursingProjectVO = nursingProjectService.getById(id);
        return ResponseResult.success(nursingProjectVO);
    }

    /**
     * 更新护理项目信息
     *
     * @param nursingProjectDTO 护理项目数据传输对象
     * @return 操作结果
     */
    @PutMapping
    @ApiOperation(value = "更新护理项目信息", notes = "根据传入的数据更新护理项目")
    public ResponseResult update(
            @ApiParam(value = "护理项目数据传输对象", required = true)
            @RequestBody NursingProjectDto nursingProjectDTO) {
        nursingProjectService.update(nursingProjectDTO);
        return ResponseResult.success();
    }

    /**
     * 启用/禁用护理项目
     *
     * @param id 护理项目编号
     * @param status 状态：0-禁用，1-启用
     * @return 操作结果
     */
    @PutMapping("/{id}/status/{status}")
    @ApiOperation(value = "启用/禁用护理项目", notes = "根据护理项目编号启用或禁用该护理项目")
    public ResponseResult enableOrDisable(
            @ApiParam(value = "护理项目编号", required = true)
            @PathVariable Long id,

            @ApiParam(value = "状态：0-禁用，1-启用", required = true)
            @PathVariable Integer status) {
        nursingProjectService.enableOrDisable(id, status);
        return ResponseResult.success();
    }

    /**
     * 删除护理项目信息
     *
     * @param id 护理项目编号
     * @return 操作结果
     */
    @DeleteMapping("/{id}")
    @ApiOperation(value = "删除护理项目信息", notes = "根据护理项目编号删除护理项目信息")
    public ResponseResult deleteById(
            @ApiParam(value = "护理项目编号", required = true)
            @PathVariable("id") Long id) {
        NursingProjectVo nursingProjectVO = nursingProjectService.getById(id);
        if (nursingProjectVO == null) {
            return ResponseResult.error();
        }
        nursingProjectService.deleteById(id);
        return ResponseResult.success();
    }

    /**
     * 查询所有护理项目
     *
     * @return 所有护理项目列表
     */
    @GetMapping("/all")
    @ApiOperation(value = "查询所有护理项目", notes = "获取所有的护理项目信息")
    public ResponseResult<List<NursingProjectVo>> getAllNursingProject() {
        return ResponseResult.success(nursingProjectService.listAll());
    }
}